﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using NFL.DataAccess;

namespace NFL.Loader
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            InitForm();
        }

        private void InitForm()
        { 
            // Bind week to ddlWeek
            DataAccess.DataAccess da = new DataAccess.DataAccess();
            ddlWeek.SelectedIndex = da.GetWeekID(DateTime.Now) - 1;

            // Deselect drop down lists
            ddlDocument.SelectedIndex = -1;

            // Clear data
            txtFile.Text = string.Empty;

        }

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            // Create OpenFileDialog 
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

            // Set filter for file extension and default file extension 
            dlg.DefaultExt = ".csv";
            dlg.Filter = "CSV Files (*.csv)|*.csv|TXT Files (*.txt)|*.txt";
            //dlg.Filter = "CSV Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";

            // Display OpenFileDialog by calling ShowDialog method 
            Nullable<bool> result = dlg.ShowDialog();


            // Get the selected file name and display in a TextBox 
            if (result == true)
            {
                // Open document 
                string filename = dlg.FileName;
                txtFile.Text = filename;
            }
        }

        private void btnImport_Click(object sender, RoutedEventArgs e)
        {
            // Validate data
            if (ddlDocument.SelectedIndex < 0)
            {
                MessageBox.Show("Must Select Document Type", "Warning", MessageBoxButton.OK);
                return;
            }

            if (ddlWeek.SelectedIndex < 0)
            {
                MessageBox.Show("Must Select Week", "Warning", MessageBoxButton.OK);
                return;
                
            }

            if (string.IsNullOrEmpty(txtFile.Text.Trim()) || !System.IO.File.Exists(txtFile.Text.Trim()))
            {
                MessageBox.Show("File doesn't exist", "Warning", MessageBoxButton.OK);
                return;

            }


            // Get data from file
            string[] fileData = System.IO.File.ReadAllLines(txtFile.Text.Trim());
            string docType = ddlDocument.Text;
            int weekID = Convert.ToInt32(ddlWeek.Text);

            DataLoad dl = new DataLoad();
            if (docType.ToLower() == "games")
            {
                if (dl.LoadWeeklyGames(weekID, fileData))
                {
                    MessageBox.Show("Import Successfull", "Success", MessageBoxButton.OK);
                }
                else
                {
                    MessageBox.Show("Import Error Occurred", "ERROR", MessageBoxButton.OK);
                }
            }
            else
            {
                if (dl.LoadWeeklyPicks(weekID, fileData))
                {
                    MessageBox.Show("Import Successfull", "Success", MessageBoxButton.OK);
                }
                else
                {
                    MessageBox.Show("Import Error Occurred", "ERROR", MessageBoxButton.OK);
                }

            }

        }



    }
}
